//
//  ZCTopic.m
//  ZCBaiSi
//
//  Created by zc on 16/7/23.
//  Copyright © 2016年 zc. All rights reserved.
//

#import "ZCTopic.h"
#import <MJExtension.h>
#import "ZCComment.h"
#import "ZCUser.h"
@implementation ZCTopic
#pragma mark --MJExtension
+ (NSDictionary *)mj_objectClassInArray{
    return @{@"top_cmt":[ZCComment class]};
}


- (NSString *)created_at{
    //_created_at == 具体时间
    
    NSCalendar *calendar = [NSCalendar calendar];
    //获得发帖日期
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *createdDate = [fmt dateFromString:_created_at];
    //手机当前时间
    NSDate *nowDate = [NSDate date];
    if (createdDate.isThisYear) {//今年
        if ([calendar isDateInToday:createdDate]) {//今天
            NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
            NSDateComponents *cmps = [calendar components:unit fromDate:createdDate toDate:nowDate options:0];
            if (cmps.hour >= 1) {
                return [NSString stringWithFormat:@"%zd小时前",cmps.hour];
            }else if (cmps.minute >=1){
                return [NSString stringWithFormat:@"%zd分钟前",cmps.minute];
            }else{
                return @"刚刚";
            }
        }else if ([calendar isDateInYesterday:createdDate]){//昨天
            fmt.dateFormat = @"昨天 HH:mm:ss";
            return [fmt stringFromDate:createdDate];
        }else{//其他
            fmt.dateFormat = @"MM-dd HH:mm:ss";
            return [fmt stringFromDate:createdDate];
        }
        
    }else{//非今年
        return _created_at;
    }
}

- (CGFloat)cellHeight{
    //如果cellHeight有值，就直接返回
    if (_cellHeight) return _cellHeight;
    _cellHeight = 55;
    CGFloat textMaxW = [UIScreen mainScreen].bounds.size.width - 2 * ZCMargin;
    CGSize textMaxSize = CGSizeMake(textMaxW , MAXFLOAT);
   // CGSize textSize = [self.text sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:textMaxSize];
    CGSize textSize = [self.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
    _cellHeight += textSize.height + ZCMargin;
    //3.中间内容
    if (self.type != ZCTopicTypeWord) {//如果是视频声音图片才需要计算中间内容的高度
        //中间内容的高度 = 中间内容的宽度 * 图片的真实高度 / 图片的真实宽度
        CGFloat contentHeight = textMaxW * self.height / self.width;
        if (contentHeight > [UIScreen mainScreen].bounds.size.height) {//超长图片
            contentHeight = 200;
            self.is_bigPic = YES;
        }
        self.contentFrame = CGRectMake(ZCMargin, _cellHeight, textMaxW, contentHeight - 10);
        _cellHeight += contentHeight;
    }
    //4.最热评论
    if(self.top_cmt){//有最热评论
        //标题
        _cellHeight += 20;
        //内容
        NSString *contentStr = [NSString stringWithFormat:@"%@ :%@",self.top_cmt.user.username,self.top_cmt.content];
//        CGSize contentSize = [contentStr sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:textMaxSize];
        CGSize contentSize = [contentStr boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
        _cellHeight += contentSize.height;
    }
    //底部工具条
    _cellHeight += 35 + ZCMargin;
    return _cellHeight + 10;
    
}
@end
